package com.frame.work.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frame.work.entity.TemperatureControlLog;
import com.frame.work.mapper.TemperatureControlLogMapper;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author wjming
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TemperatureControlLogService extends ServiceImpl<TemperatureControlLogMapper, TemperatureControlLog> {
    private final TemperatureControlLogMapper mapper;

    public IPage<TemperatureControlLog> getPage(String gatewayMac, String deviceMac, Integer pageNum, Integer pageSize, Long time) {
        IPage<TemperatureControlLog> temperatureControlLogPage = new Page<>(pageNum, pageSize, true);
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // new Date()为获取当前系统时间
        String format = df.format(new Date(time));
        String start = format + " 00:00:00";
        String end = format + " 23:59:59";

        IPage<TemperatureControlLog> temperatureControlLogIPage = mapper.selectPage(temperatureControlLogPage, Wrappers.<TemperatureControlLog>lambdaQuery()
                .eq(TemperatureControlLog::getGatewayMac, gatewayMac).eq(TemperatureControlLog::getDeviceMac, deviceMac)
                .between(TemperatureControlLog::getCreateTime, start, end)
                .orderByDesc(TemperatureControlLog::getCreateTime));

        return temperatureControlLogIPage;
    }
}
